package com.cb.kfc.dao;

import com.cb.kfc.domain.Member;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface IMemberDao {
    //查找单个会员类别
    @Select("select * from member where id = #{id}")
    Member findOne(String id);

    @Select("select * from member")
    List<Member> findAll();


    @Insert("insert into member(name,nickname,phoneNum) values(#{name},#{nickname},#{phoneNum})")
    void save(Member member);

    //删除单个，或者批量删除
    @Delete("<script>delete from member where id in <foreach collection='array' item='id' open='(' separator=',' close=')'>#{id}</foreach> </script>")
    Integer delete(String[] ids);

    @Select("select * from member where id =#{id}")
    @Results({
            @Result(id=true,column = "id",property = "id"),
            @Result(column = "name",property = "name"),
            @Result(column = "nickname",property = "nickname"),
            @Result(column = "phoneNum",property = "phoneNum"),
            @Result(column = "id",property = "friend",javaType = java.util.List.class,many = @Many(select = "com.cb.kfc.dao.IFriendDao.findByMemberId")),
//最后一行，column = "id"就是使用本表member的id，通过中间表，查询friend，是一个多对多的关系，返回多个，返回集合List
    })
    Member findById(String id);

    @Insert("insert into ORDER_FRIEND(orderid,friendid) values(#{orderid},#{friendid})")
    void give(@Param("orderid") String orderid, @Param("friendid") String friendid);

    @Delete("<script>delete from ORDER_FRIEND where friendid in <foreach collection='array' item='id' open='(' separator=',' close=')'>#{id}</foreach> </script>")
    Integer deleteFriend(String[] ids);

    //更新
    @Update("update member set  name=#{name},phoneNum=#{phoneNum}，nickname=#{nickname} where id =#{id} ")
    void update(Member member);
}
